package com.start.controller;

import com.start.domain.Girl;
import com.start.repostiory.GirlRepostiory;
import com.start.service.GirlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by starmayu on 2017/9/25.
 *
 */
@RestController
public class GirlController {

    @Autowired
    private GirlRepostiory girlRepostiory;

    @Autowired
    private GirlService girlService;


    //查询列表所有
    @GetMapping(value = "/girls")
    public List<Girl> girlList(){
        return girlRepostiory.findAll();
    }

    @PostMapping(value = "/girls")
    public Girl girlAdd(@Valid Girl girl, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return null;
        }
        girl.setAge(girl.getAge());
        girl.setCupSize(girl.getCupSize());
        return girlRepostiory.save(girl);

    }
    //查询一条
//    @GetMapping(value = "/girls/{id}")
//    public Girl girlFind(@PathVariable("id") Integer id){
//        return girlRepostiory.findOne(id);
//    }

    //更新
    @PutMapping(value = "/girls/{id}")
    public Girl girlUpdate(@PathVariable("id") Integer id,
                           @PathVariable("cupSize") String cupSize,
                           @PathVariable("age") Integer age){
        Girl girl = new Girl();
        girl.setId(id);
        girl.setAge(age);
        girl.setCupSize(cupSize);
        return girlRepostiory.save(girl);
    }

    //删除
    @DeleteMapping(value = "/girls/{id}")
    public void girlDelete(@PathVariable("id") Integer id){
        girlRepostiory.delete(id);
    }

    @GetMapping(value = "/girls/age/{age}")
    public List<Girl> girlListByAge(@PathVariable("age") Integer age){
        return girlRepostiory.findByAge(age);
    }

    @GetMapping(value = "/girls/tow")
    public void girlTwo(){
        girlService.insertTwo();
    }


}
